package com.siss.tenancy.elastic.controller;


import com.siss.tenancy.constant.RocketMqEnum;
import com.siss.tenancy.elastic.client.UserClient;
import com.siss.tenancy.elastic.config.RocketMqSender;
import com.siss.tenancy.elastic.service.SissMonitorEsImp;
import com.siss.tenancy.elastic.utils.RedisUtil;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("elasticSearch")
@Api(value = "SissMonitorElasticSearchController", description = "elasticSearch相关接口")
public class SissMonitorElasticSearchController {

    Logger logger = LoggerFactory.getLogger(SissMonitorElasticSearchController.class);

    @Autowired
    SissMonitorEsImp sissMonitorEsImp;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    private RocketMqSender rocketMqSender;

    @Autowired
    UserClient userClient;


    @ApiOperation(httpMethod = "GET", value = "分页es", notes = "分页es")
    @GetMapping(value = "page")
    public Object selectProgress(@ApiParam("输入框内容") @RequestParam(value = "keyWord", required = false)String keyWord,
                                 @ApiParam("搜索类型，1系统搜索，2楼盘搜索") @RequestParam(value = "type", required = false)Integer type,
                                 @ApiParam("用户类型：1机构2分支机构3人") @RequestParam(value = "actorTypes", required = false)String[] actorTypes,
                                 @ApiParam("区域，根据字典传入字典值") @RequestParam(value = "districts", required = false)String[] districts,
                                 @ApiParam("星级级别1 2 3 4 5 0") @RequestParam(value = "starLevels", required = false)Integer[] starLevels,
                                 @ApiParam("详见字典：备案状态") @RequestParam(value = "recordStatus", required = false)Integer[] recordStatus,
                                 @ApiParam("是否备案，true false，当过滤为全选时，去掉次过滤条件") @RequestParam(value = "record", required = false)Boolean record,
                                 @ApiParam("是否是高亮显示") @RequestParam(value = "hilight", required = false)Boolean hilight,
                                 @ApiParam("是否是黑名单：1是0否，当过滤为全选时，去掉次过滤条件") @RequestParam(value = "black", required = false)Integer black,
                                 @ApiParam("加盟类型，1直营2加盟，当过滤为全选时，去掉次过滤条件") @RequestParam(value = "leagueType", required = false)Integer leagueType,
                                 @ApiParam("每页数量") @RequestParam(value = "pageSize", required = false) int pageSize,
                                 @ApiParam("页码") @RequestParam(value = "pageIndex", required = false) int pageNo,
                                 @ApiParam("是否分页：默认是false，前端需要根据需要传此参数") @RequestParam(value = "pageEable", defaultValue = "false")boolean pageEable) throws IOException {
        return sissMonitorEsImp.selectByPage(hilight,keyWord, type, actorTypes, districts,
                 starLevels,recordStatus,record,black,leagueType,
         pageSize,  pageNo, pageEable);
    }



    @GetMapping(value = "rocketmq")
    public void rocketmq(){
        logger.info("测试logback同步日志到es");
        Map<String, Object> headers = new HashMap<>();
        headers.put(MessageConst.PROPERTY_TAGS, "tagStr");
        Map<String, Object> content = new HashMap<>();
        content.put("ceshi","收到");
        Map<String, Object> aa = new HashMap<>();
        aa.put("sonMap","呵呵");
        content.put("map","收到");
        content.put("son",aa);
        Message message = MessageBuilder.withPayload(content).setHeader(MessageConst.PROPERTY_TAGS, "tagStr")
                .setHeader("head","疼")
                .setHeader(RocketMQHeaders.TRANSACTION_ID,"123")
                .build();
        rocketMqSender.sendTransaction(RocketMqEnum.Topic.MESSAGE.getCode(),message,"test");
    }

    @GetMapping(value = "seata")
    @GlobalTransactional(timeoutMills = 30000, name = "myGlobalText",rollbackFor = Exception.class)
    public String seata(@RequestParam("noTrans") Integer noTrans){
        //不从缓存获取，直接写死为常量
        logger.info("Order Service Begin ... xid: " + RootContext.getXID());
        sissMonitorEsImp.testSeata(noTrans);
        userClient.dbInfo(noTrans);
        return "sucess";
    }

}
